package khtn.mylocation;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class Location extends AppRecord {

	private Integer id;
	private String name;
	private String description;
	private Integer category;
	private Double lat;
	private Double lng;
	
	private String[] allColumns = {
			SQLiteHelper.LOCATION_ID,
			SQLiteHelper.LOCATION_NAME,
			SQLiteHelper.LOCATION_DESCRIPTION,
			SQLiteHelper.LOCATION_CATEGORY,
			SQLiteHelper.LOCATION_LAT,
			SQLiteHelper.LOCATION_LNG
		};
	
	public Location(Context context) {
		super(context);
	}
	
	public Integer getId() {
		return id;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getName() {
		return this.name;
	}
	
	public void setDescription(String description){
		this.description = description;
	}
	
	public String getDescription() {
		return this.description;
	}
	
	public void setLng(Double lng){
		this.lng = lng;
	}
	
	public Double getLng() {
		return this.lng;
	}
	
	public void setLat(Double lat){
		this.lat = lat;
	}
	
	public Double getLat() {
		return this.lat;
	}
	
	public void setCategory(Integer category){
		this.category = category;
	}
	
	public Integer getCategory() {
		return this.category;
	}
	
	public Location create() {
		this.open();
		ContentValues values = new ContentValues();
		values.put(SQLiteHelper.LOCATION_NAME, this.name);
		values.put(SQLiteHelper.LOCATION_DESCRIPTION, this.description);
		values.put(SQLiteHelper.LOCATION_CATEGORY, this.category);
		values.put(SQLiteHelper.LOCATION_LAT, this.lat);
		values.put(SQLiteHelper.LOCATION_LNG, this.lng);
		
		long insertId = database.insert(SQLiteHelper.TABLE_LOCATION, null, values);
		
		Cursor cursor = database.query(SQLiteHelper.TABLE_LOCATION,
				allColumns, SQLiteHelper.LOCATION_ID + " = " + insertId,
				null, null, null, null);
		cursor.moveToFirst();
		Location newLocation = cursorToLocation(cursor);
		cursor.close();
		this.close();
		return newLocation;
	}
	
	public List<Location> getAll() {
		this.open();
		Cursor cursor = database.query(SQLiteHelper.TABLE_LOCATION,
				allColumns, null, null, null, null, null);
		List<Location> locations = new ArrayList<Location>();
		while(cursor.moveToNext())
		{
			Location data = cursorToLocation(cursor);
			locations.add(data);
		}
		this.close();
		return locations;
	}
	
	private Location cursorToLocation(Cursor cursor) {
		Location location = new Location(null);
		location.id = cursor.getInt(cursor.getColumnIndex(SQLiteHelper.LOCATION_ID));
		location.name = cursor.getString(cursor.getColumnIndex(SQLiteHelper.LOCATION_NAME));
		location.description = cursor.getString(cursor.getColumnIndex(SQLiteHelper.LOCATION_DESCRIPTION));
		location.category = cursor.getInt(cursor.getColumnIndex(SQLiteHelper.LOCATION_CATEGORY));
		location.lng = cursor.getDouble(cursor.getColumnIndex(SQLiteHelper.LOCATION_LNG));
		location.lat = cursor.getDouble(cursor.getColumnIndex(SQLiteHelper.LOCATION_LAT));
		return location;
	}
}
